package lib

import (
	"fmt"
	"image/color"

	"github.com/mojocn/base64Captcha"
)

// 设置自带的 store
var store = base64Captcha.DefaultMemStore

// 生成验证码
func MakeCaptcha(height, width, length int) (id, b64s string, err error) {
	var driver base64Captcha.Driver
	// 配置验证码信息
	driverString := base64Captcha.DriverString{
		Height:          height,
		Width:           width,
		NoiseCount:      0,
		ShowLineOptions: 2 | 4,
		Length:          length,
		Source:          "1234567890qwertyuioplkjhgfdsazxcvbnm",
		BgColor: &color.RGBA{
			R: 3,
			G: 102,
			B: 214,
			A: 125,
		},
		Fonts: []string{"wqy-microhei.ttc"},
	}
	//ConvertFonts 按名称加载字体

	driver = driverString.ConvertFonts()
	//创建 Captcha
	captcha := base64Captcha.NewCaptcha(driver, store)
	//Generate 生成随机 id、base64 图像字符串

	lid, lb64s, _, lerr := captcha.Generate()
	return lid, lb64s, lerr
}

// 验证captcha是否正确
func VerifyCaptcha(id string, VerifyValue string) bool {
	fmt.Println(id, VerifyValue)
	if store.Verify(id, VerifyValue, true) {
		return true
	} else {
		return false
	}
}
